package cn.tedu.v1.v3_1;

import java.io.*;
import java.net.*;
import java.util.*;

/**
 * 聊天室Version 3
 *
 */
public class Server {
    private ServerSocket serverSocket;
    //优化1.端口号提前声明:私有属性
    private static final Integer PORT=4869;
    public Server() {
        try {
            serverSocket = new ServerSocket(PORT);
        } catch (BindException e) {
            System.out.println("请检查端口号，被占用~");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

  public void start() {
     String ip=null;
     try {
         while (true) {
             System.out.println("等待客户端连接");
             Socket socket = serverSocket.accept();


             new Thread(new ClientHandler(socket)).start();
         }
     } catch (IOException e) {
         System.out.println(ip+"客户端连接断开连接~");
     } finally {
         if (serverSocket!=null&&!serverSocket.isClosed()){
             try {
                 serverSocket.close();
             } catch (IOException e) {
                 System.out.println("服务器关闭失败~~~");
             }
             System.out.println("服务器关闭成功~~Over");
         }
     }
 }

 public static void main(String[] args) {
     new Server().start();
 }
//ClientHandler多线程类：控制客户端发来的通讯
 private class ClientHandler implements Runnable {
     private Socket socket;
     private String ip;
    //Server---CS类---输入   输出   流  关闭
    //Clent---start()---输入   输出   流  关闭
     private PrintWriter pw;
     private BufferedReader br;


     public ClientHandler(Socket s){
         //1,明确接入客户端 通讯类 Socket
         this.socket=s;
         //2,给明确接入客户端，IP地址
        ip=socket.getInetAddress().getHostAddress();
         //3,给明确接入客户端，读写功能
         try {
             br = new BufferedReader( new InputStreamReader(socket.getInputStream(), "UTF8"));
             pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), "UTF8")), true);
         } catch (IOException e) {
             System.out.println("客户端断开连接了~");
         }
     }

     public void run() {
      try {

          new Thread(()->{
              try {
                  String message;
                  while ((message = br.readLine()) != null) {//读入客户端
                      System.out.println(ip+"客户端说：" + message);
                  //1.6 增加一句话--，每次客户端发给服务器内容
                      System.out.print("服务器请输入：");
                  }
              } catch (IOException e) {
                  System.out.println("客户端发来的消息断开了~");
              }
          }).start();
          Scanner input=new Scanner(System.in);
          while(true){
              System.out.println("服务器请输入：");
              String sme= input.nextLine();
              pw.println("服务器说；"+sme);
          }

      } finally {
       if (pw!= null)
       pw.close();
       if (br !=null)
       try {
           br.close();
       } catch (IOException e) {
           System.out.println("读入流断开连接");
       }

       if (socket!=null&&!socket.isClosed()){
           System.out.println(ip+"客户端退出服务器链接~");
           try {
               socket.close();
           } catch (IOException e) {
               System.out.println(ip+"客户端退出链接异常~");
           }
       }
             }

          }
       }
         }
